<?php

class Login extends Admin {
    
    public function __construct() {
		parent::__construct();
	}
	
    public function index() {

	    $url = isset($_GET['url']) && $_GET['url'] ? urldecode($this->get('url')) : url('admin//');
		if ($this->isPostForm()) {
		    $username = $this->post('username');
		    $password = $this->post('password');
		    $result = $this->user->check_login($username, $password);
		    if ($result) {
				if ($result['site'] && $result['site'] != $this->siteid) {
                    $this->adminMsg(dr_lang('您无权管理该网站'));
                }
		        $this->session->set('user_id', (int)$result['userid']);
			    $this->adminMsg(dr_lang('登录成功'), $url, 3, 1, 1);
		    } else {
			    $this->adminMsg(dr_lang('登录失败'), url('admin/login', array('url'=>$this->get('url'))));
			}
		}

		$this->view->display('admin/login');
    }
    
    public function logout() {
        if ($this->session->is_set('user_id')) {
            $this->session->unset_userdata('user_id');
        }
        $this->adminMsg(dr_lang('退出成功'), url('admin/login'), 3, 1, 1);
    }
}